package com.sdy.resdir.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.auth.client.service.SsoService;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.resdir.biz.model.FlowHistories;
import com.sdy.resdir.biz.service.FlowHistoriesService;
import com.sdy.resdir.biz.service.RequirementReplyService;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.resdir.biz.model.RequirementReply;
import com.sdy.common.model.Response;
import com.sdy.usercenter.api.UserQueryApi;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdy.mvc.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cgj
 * @since 2020-01-03
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/requirementReply")
public class RequirementReplyController extends BaseController {
    @Autowired
    private RequirementReplyService requirementReplyService;
//    @Autowired
//    private FlowHistoriesService flowHistoriesService;
//    @Autowired
//    private SsoService ssoService;
//    @Autowired
//    private UserQueryApi userQueryApi;
//
//    @ApiOperation(value = "获取需求回复列表")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(name = "需求名称", value = "requirementName"),
//            @ApiImplicitParam(name = "需求状态(2.已回复 3.待处理 4.已流转)", value = "requirementState")
//    })
//    @GetMapping("/getRequirementList")
//    public Response getRequirementList(String requirementName, Integer requirementState) {
//        List<RequirementReply> requirementReplyList = requirementReplyService.list(new LambdaQueryWrapper<RequirementReply>()
//                .like(requirementName != null, RequirementReply::getRequirementName, requirementName)
//                .eq(requirementState != null, RequirementReply::getRequirementState, requirementState)
//                .orderByDesc(RequirementReply::getId));
//        return Response.success(requirementReplyList);
//    }
//
//
//    @ApiOperation("新增需求单")
//    @PostMapping("/addReqiurement")
//    public Response addReqiurement(@RequestBody @ApiParam(name = "requirementName & requirementType & instructionsDetail", value = "需求名称，需求类型(1.数据需求 2.问题反馈)，详细说明") RequirementReply requirementReply) {
//        boolean save = requirementReplyService.save(requirementReply);
//        return save ? Response.success() : Response.error();
//    }
//
//    @ApiOperation("获取单条需求详情及流转记录")
//    @ApiImplicitParam(name = "requirementId", value = "需求id")
//    @GetMapping("/getRequirementOrReplyDetail")
//    public Response getRequirementOrReplyDetail(Integer requirementId) {
//        RequirementReply requirementReply = requirementReplyService.getById(requirementId);
//        List<FlowHistories> flowHistories = flowHistoriesService.list(new LambdaQueryWrapper<FlowHistories>()
//                .eq(FlowHistories::getRequirementId, requirementId));
//        Map<String, Object> requirementOrReplyDetail = new HashMap<>(16);
//        requirementOrReplyDetail.put("requirementReply", requirementReply);
//        requirementOrReplyDetail.put("flowHistories", flowHistories);
//        return Response.success(requirementOrReplyDetail);
//    }
//
//    @ApiOperation("需求单流转或回复")
//    @PostMapping("/RequirementFlow")
//    public Response RequirementFlow(@RequestBody @ApiParam(name = "requirementId & flowReceiveDeptId & flowReceiveDeptName & flowReason & requirementState",
//            value = "需求id，流转接收部门id,流转接受部门名称，流转发起原因，需求状态(1.待回复 2.已回复 3.待处理 4.已流转)") HttpServletRequest request, FlowHistories flowHistories) throws BizException {
//        Integer userId = ssoService.getUserId(request);
//        Integer ownDeptId = userQueryApi.getUser(userId).getOwnDeptId();
//        String ownDeptName = userQueryApi.getUser(userId).getOwnDeptName();
//        Assert.isNull(userId, "用户未登录");
//        List<RequirementReply> requirementReplyList = requirementReplyService.list(new LambdaQueryWrapper<RequirementReply>().eq(RequirementReply::getId, flowHistories.getRequirementId()));
//        Assert.isTrue(requirementReplyList.isEmpty(), "该需求单不存在");
//        List<Integer> requirementIdList = requirementReplyList.stream().map(RequirementReply::getId).collect(Collectors.toList());
//        List<FlowHistories> flowHistoriesList = flowHistoriesService.list(new LambdaQueryWrapper<FlowHistories>()
//                .in(FlowHistories::getRequirementId, requirementIdList));
//        for (RequirementReply requirementReply : requirementReplyList) {
//            if(requirementReply.getRequirementState() == 2){
//                return Response.error("该需求已被" + requirementReply.getReplyDeptName() + "回复，不可流转");
//            }else if (requirementReply.getRequirementState() == 4){
//                return Response.error("该资源已流转，不可重复流转");
//            }
//        }
//        for (FlowHistories oneFlowHistories : flowHistoriesList) {
//            Assert.isTrue(oneFlowHistories.getFlowInitateDeptId().equals(ownDeptId), "该需求已流转，不可重复流转");
//        }
//        flowHistories.setFlowInitateDeptId(ownDeptId).setFlowInitateDeptName(ownDeptName).setFlowStartTime(new Date());
//        boolean saveFlowHistories = flowHistoriesService.save(flowHistories);
//        boolean saveRequirementReply = requirementReplyService.save(new RequirementReply()
//                .setReplyDeptId(ownDeptId)
//                .setReplyDeptName(ownDeptName)
//                .setRequirementType(3));
//        return saveFlowHistories && saveRequirementReply ? Response.success("需求流转成功") : Response.error("需求流转失败");
//    }

}
